package com.x.wx.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.x.wx.model.WxAccount;
import com.x.wx.rpc.WxAccountRpcService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.MenuApi;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import com.x.wx.model.WxMenu;
import com.x.wx.rpc.WxMenuRpcService;

/**
 * 微信菜单
 */
@RestController
@RequestMapping("/wxMenu")
public class WxMenuController extends BaseController {

	@Reference
	private WxMenuRpcService wxMenuRpcService;

	@Reference
	private WxAccountRpcService wxAccountRpcService;

	@RequestMapping(value="/selectWxMenuList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel selectWxMenuList(WxMenu wxMenu) {
		if(StringUtils.isEmpty(wxMenu.getAccountId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "公众号ID不能为空！");
		}
		List<WxMenu> wxMenuList = wxMenuRpcService.selectWxMenuList(wxMenu);
		if  (wxMenuList.size() > 1 ){
			List<Map<String, Object>> parentMenuList = new ArrayList<>();
			for(WxMenu menu:wxMenuList){
				if(menu.getMenuParentId().equals("0")){
					Map<String, Object> parentMap = JSON.parseObject(JSON.toJSONString(menu), Map.class);
					parentMenuList.add(parentMap);
				}
			}
			for(Map<String, Object> parentMap : parentMenuList) {
				List<WxMenu> childMenuList = new ArrayList<>();
				for (WxMenu menu : wxMenuList) {
					if (parentMap.get("menuId").equals(menu.getMenuParentId())) {
						childMenuList.add(menu);
					}
				}
				parentMap.put("childMenu", childMenuList);
			}
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", parentMenuList);
		}else{
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", wxMenuList);
		}

	}


	@RequestMapping(value="/insertWxMenu", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertWxMenu(WxMenu wxMenu) {
		if(StringUtils.isEmpty(wxMenu.getAccountId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "公众号ID不能为空！");
		}
		if (StringUtils.isEmpty(wxMenu.getMenuParentId())){
			wxMenu.setMenuParentId("0");
		}
		wxMenuRpcService.insertWxMenu(wxMenu);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功！");
	}

	@RequestMapping(value= "/updateWxMenu", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel updateWxMenu(WxMenu wxMenu) {
		if(StringUtils.isEmpty(wxMenu.getAccountId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "公众号ID不能为空！");
		}
		if (StringUtils.isEmpty(wxMenu.getMenuParentId())){
			wxMenu.setMenuParentId("0");
		}
		wxMenuRpcService.updateWxMenu(wxMenu);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "更新成功！");
	}

	@RequestMapping(value= "/deleteWxMenu", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel deleteWxMenu(WxMenu wxMenu) {
		if(StringUtils.isEmpty(wxMenu.getAccountId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "公众号ID不能为空！");
		}
		wxMenuRpcService.deleteWxMenu(wxMenu);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "删除成功！");
	}

	/**
	 * 同步菜单到微信
	 * @param
	 * @return
	 */
	@RequestMapping("/synWxMenu")
	public ResultModel synWxMenu(String accountId) {
		WxAccount wxAccount = new WxAccount();
		wxAccount.setWxAccountId(accountId);
		wxAccount = wxAccountRpcService.selectWxAccountById(wxAccount);
		WxMenu menu = new WxMenu();
		menu.setMenuParentId("0");
		menu.setAccountId(accountId);
		List<WxMenu> menus = wxMenuRpcService.selectWxMenuList(menu);
		Map<String, Object> menuMap = new HashMap<>();
		List<Map<String, Object>> buttonList = new ArrayList<>();
		for(WxMenu wxMenu : menus){
			Map<String, Object> masterMap = new HashMap<>();
			masterMap.put("name", wxMenu.getMenuName());
			if(wxMenu.getMenuType().equals("master")){
				menu = new WxMenu();
				menu.setMenuParentId(wxMenu.getMenuId());
				List<WxMenu> childrenMenu = wxMenuRpcService.selectWxMenuList(menu);
				if(childrenMenu != null && childrenMenu.size() > 0){
					List<Map<String, String>> childrenList = new ArrayList<>();
					for(WxMenu childMenu : childrenMenu){
						Map<String, String> childMap = new HashMap<>();
						childMap.put("type", childMenu.getMenuType());
						childMap.put("name", childMenu.getMenuName());
						if(childMenu.getMenuType().equals("click")){
							childMap.put("key", childMenu.getMenuKey());
						}else if(childMenu.getMenuType().equals("view")){
							childMap.put("url", childMenu.getMenuUrl());
						}else if(childMenu.getMenuType().equals("media_id")){
							childMap.put("media_id", childMenu.getMenuMediaId());
						}
						childrenList.add(childMap);
					}
					masterMap.put("sub_button", childrenList);
				}
			}else{
				masterMap.put("type", wxMenu.getMenuType());
				if(wxMenu.getMenuType().equals("click")){
					masterMap.put("key", wxMenu.getMenuKey());
				}else if(wxMenu.getMenuType().equals("view")){
					masterMap.put("url", wxMenu.getMenuUrl());
				}else if(wxMenu.getMenuType().equals("media_id")){
					masterMap.put("media_id", wxMenu.getMenuMediaId());
				}
			}
			buttonList.add(masterMap);
		}
		menuMap.put("button", buttonList);
		ApiResult res = MenuApi.createMenu(JSON.toJSONString(menuMap), wxAccount.getWxAppId());
		if(res.isSucceed()){
			return new ResultModel(ResultCode.RESULT_SUCCESS, "同步成功！");
		}else{
			return new ResultModel(ResultCode.RESULT_FAIL, res.getErrorMsg(), res);
		}
	}

}
